package org.elephas.service;

import org.elephas.model.Blog;
import org.elephas.model.BlogConfiguration;
import org.elephas.model.User;
import org.elephas.persistence.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import salve.contract.NotNull;

/**
 * Singleton to handle top level objects like {@link User}, {@link Blog}.
 * 
 * @author Gerolf Seitz
 * 
 */
@Service
public class Elephas {
	@Autowired
	private Dao dao;

	/**
	 * Saves the given {@link Blog} and sets the default
	 * {@link BlogConfiguration#} in case there is no configuration set yet.
	 * 
	 * @param toSave
	 *            the {@link Blog} to save
	 */
	@Transactional
	public void saveBlog(@NotNull Blog toSave) {
		if (toSave.getConfiguration() == null) {
			BlogConfiguration config = new BlogConfiguration();
			config.setBlog(toSave);
		}
		dao.saveOrUpdate(toSave);
	}

	/**
	 * Deletes the given {@link Blog} instance.
	 * 
	 * @param toDelete
	 *            the {@link Blog} to delete.
	 */
	@Transactional
	public void deleteBlog(@NotNull Blog toDelete) {
		dao.delete(toDelete);
	}

	/**
	 * Saves the given {@link User}.
	 * 
	 * @param toSave
	 *            the {@link User} to save
	 */
	@Transactional
	public void saveUser(@NotNull User toSave) {
		dao.saveOrUpdate(toSave);
	}

	/**
	 * Deletes the given {@link User}.
	 * 
	 * @param toDelete
	 *            the {@link User} to delete
	 */
	@Transactional
	public void deleteUser(@NotNull User toDelete) {
		dao.delete(toDelete);
	}
}
